package com.yichen;

/**
 * @Classname Utils
 * @Description
 * @Author caozhenhao001
 * @Date 2024/6/4
 * @Version
 */
public class Utils {

    /**
     * 下划线转驼峰
     * @param snakeCase
     * @return
     */
    public static String convertSnakeToCamel(String snakeCase) {
        if(snakeCase == null || snakeCase.isEmpty()) {
            return snakeCase;
        }

        StringBuilder result = new StringBuilder();
        boolean convertNext = false;

        for (char ch : snakeCase.toCharArray()) {
            if (ch == '_') {
                convertNext = true;
            } else {
                if (convertNext) {
                    result.append(Character.toUpperCase(ch));
                    convertNext = false;
                } else {
                    result.append(ch);
                }
            }
        }

        return result.toString();
    }

    /**
     * 驼峰转下划线
     * @param camelCase
     * @return
     */
    public static String convertCamelToSnake(String camelCase) {
        if (camelCase == null || camelCase.isEmpty()) {
            return camelCase;
        }

        StringBuilder snakeCase = new StringBuilder();
        char[] chars = camelCase.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    snakeCase.append('_');
                }
                snakeCase.append(Character.toLowerCase(c));
            } else {
                snakeCase.append(c);
            }
        }

        return snakeCase.toString();
    }

    /**
     * 字符串首字符大写
     * @param s
     * @return
     */
    public static String capFirst(String s) {
        if(s == null || s.length() == 0) {
            return s;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if(i == 0) {
                stringBuilder.append(Character.toUpperCase(s.charAt(i)));
            } else {
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

}
